package com.web.controller;

import com.alibaba.fastjson2.JSON;
import com.web.entity.ResponseResult;
import com.web.entity.Student;
import com.web.service.StudentService;
import com.web.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/findStudentByName")
public class FindStudentByName extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置内容类型
        response.setContentType("application/json;charset=UTF-8");
        //获取需要查询信息的name
        String name = request.getParameter("name");
        //创建业务逻辑层对象
        StudentService studentService = new StudentServiceImpl();
        //判断那么是否为空
        if (name != null && !name.equals("")){
            //调用业务逻辑
            List<Student> student = studentService.findStudentByName(name);
            ResponseResult result = new ResponseResult(200, "通过学生姓名模糊查询成功！", student);
            response.getWriter().write(JSON.toJSONString(result));
        }else {
            response.getWriter().write("msg:name为空！");
        }
    }
}
